﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Linq;
using SAKS.Models;
using SAKS.ViewModels;
using SAKS.IServices;

using NHibernate;
using NHibernate.Linq;

namespace SAKS.Services
{
    public class UserServices : IUserServices
    {
        public NHibernate.ISession Session { get; set; }

        public string UserName { get; set; }

        public IList<Project> GetUserProjects()
        {
            return GetUserProjects(UserName);
        }
        private User curruntUser;
        private User CurruntUser {
            get {
                if(curruntUser == null)
                    curruntUser = Session.Query<User>().Where(u => u.UserName == UserName).SingleOrDefault();
                return curruntUser;
            }
        }
        public IList<Project> GetUserProjects(string username)
        {
            return Session.QueryOver<User>().Where(u => u.UserName == username).SingleOrDefault().Projects.ToList();           
        }

        private int? GetUserProjectsCount(string UserName)
        {
            if (string.IsNullOrEmpty(UserName))
                throw new ArgumentException("UserName is null or empty");
            return Session.Query<User>().Where(u => u.UserName == UserName).SingleOrDefault().Projects.Count;
        }




        public IList<String> GetAllUsers(int? pageIndex, int? pageSize, out long totalRecords)
        {
            totalRecords = Session.Query<User>().Count();

            int _pageIndex = (int)pageIndex;
            int _pageSize = (int)pageSize;

            var list = (from p in Session.Query<User>()
               .OrderBy(p => p.UserName)
                        select p.UserName)
               .Skip((_pageIndex - 1) * _pageSize)
               .Take(_pageSize);

            return list.ToList();
        }

        #region PersonInfo
        public PersonInfo GetPersonInfoByUserName(string username)
        {
            if (String.IsNullOrEmpty(username)) throw new ArgumentException("username 不可以为空或null");
            return Session.QueryOver<PersonInfo>().Where(u => u.UserName == username).SingleOrDefault();
        }

        public IList<PersonInfo> GetPersonInfoByRealName(string realname)
        {
            if (String.IsNullOrEmpty(realname)) throw new ArgumentException("username 不可以为空或null");
            return Session.QueryOver<PersonInfo>().Where(p => p.Name == realname).List();
        }
        public void AddPersonInfo(string username ,PersonInfo info)
        {
            if (info == null)
                throw new ArgumentException("personinfo");
            info.UserName = username;
            Session.Save(info);
        }
        public bool UpdatePersonInfo(PersonInfo info)
        {
            var personinfo = Session.QueryOver<PersonInfo>().Where(p => p.UserName == info.UserName).SingleOrDefault();
            if (personinfo != null)
            {
                Session.Delete(personinfo);
                Session.Flush();
            }
            Session.Save(info);
            return true;
        }

        #endregion


    }
}